<?php

include '../templateSys/SQL.php';
include '../templateSys/Settings.php';
include '../templateSys/manager_util.php';
include '../templateSys/Admin.php';
include_once 'AjaxRequestProcessor.php';


global $link;
$link = getLink();
checkUserAdmin();

//prace s objednavkou
if (isset($_GET["smazatObj"])) {
    setOrderStav(Settings::$order_stav_smazana, $_GET["smazatObj"]);
    echo vytbvorTabulkuObjednavky($_GET["smazatObj"]);
}
if (isset($_GET["zpracovavase"])) {
    setOrderStav(Settings::$order_stav_zpracovavaSe, $_GET["zpracovavase"]);
    echo vytbvorTabulkuObjednavky($_GET["zpracovavase"]);
}
if (isset($_GET["cekasenaodber"])) {
    setOrderStav(Settings::$order_stav_cekaseNaOdber, $_GET["cekasenaodber"]);
    echo vytbvorTabulkuObjednavky($_GET["cekasenaodber"]);
}
if (isset($_GET["hotova"])) {
    setOrderStav(Settings::$order_stav_dokoncena, $_GET["hotova"]);
    echo vytbvorTabulkuObjednavky($_GET["hotova"]);
}
if (isset($_GET["predanoprepravci"])) {
    setOrderStav(Settings::$order_stav_dopravce, $_GET["predanoprepravci"]);
    echo vytbvorTabulkuObjednavky($_GET["predanoprepravci"]);
}

if (isset($_POST["deleteColour"])) {
    deleteColour($_POST["deleteColour"]);
    mysql_close($link);
    exit();
}

if (isset($_POST["newColour"])) {
    $name = $_POST["colourName"];
    $idcismap = $_POST["newColour"];
    createColour($idcismap, $name);
    mysql_close($link);
    exit();
}

if (isset($_POST["editSectionDelete"])) {
    if (getUser()->getRole() == "admin") {
        deleteTree($_POST["editSectionDelete"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_POST["updateText"]) && isset($_POST["name"]) && isset($_POST["data"])) {
    if (getUser()->getRole() == "admin") {
        updateText($_POST["name"], $_POST["data"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_POST["updateInt"]) && isset($_POST["name"]) && isset($_POST["data"])) {
    if (getUser()->getRole() == "admin") {
        updateInt($_POST["name"], $_POST["data"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_POST["updateImage"])) {
    if (getUser()->getRole() == "admin") {
        uploadFile("img", $_POST["name"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_POST["updateSectionTree"])) {
    if (getUser()->getRole() == "admin") {
        updateText($_POST["textName"], $_POST["nadpisSekce"]);
        if ($_POST["sectionId"] != $_POST["section"]) {
            changeSectionParent($_POST["sectionId"], getTypeIdByName($_POST["section"]));
        }
    }
    mysql_close($link);
    exit();
}

if (isset($_POST["sklad"])) {
    if (getUser()->getRole() == "admin") {
        updateInt($_POST["intName"], $_POST["mnozstvi"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_GET["addImageToColor"]) && isset($_GET["id_addimagetocolour"])) {
    if (getUser()->getRole() == "admin") {
        addImageToColor($_GET["id_addimagetocolour"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_GET["orderList"])) {
    echo ARP::spravaObjednavekAdmin();
    mysql_close($link);
    exit();
}

function addImageToColor($barvaName) {
    $name = generateName();
    $query = sprintf(SQL::$copyImage, mysql_real_escape_string($name), mysql_real_escape_string(Settings::$defaultImage));
    mysql_query($query, $GLOBALS["link"]);
    insertRecurrentId($barvaName, $name);
}

function updateSklad($jenasklade, $id, $value) {
    $query = sprintf(SQL::$update_sklad, mysql_real_escape_string($jenasklade), mysql_real_escape_string($id), mysql_real_escape_string($value));
    $result = mq($query, $GLOBALS["link"]);
}

if (isset($_POST["updateDelete"])) {
    if (getUser()->getRole() == "admin") {
        deleteItem($_POST["updateDelete"]);
    }
    mysql_close($link);
    echo "delete";
    exit();
}

if (isset($_POST["deleteDynItem"])) {
    if (getUser()->getRole() == "admin") {
        $name = $_POST["deleteDynItem"];
        deleteImg($name);
        deleteRecurrent($name);
    }
    mysql_close($link);
    echo "delete";
    exit();
}

if (isset($_POST["updateSectionDetail"])) {
    if (getUser()->getRole() == "admin") {
        updateSection($_POST["updateSection"], $_POST["section"]);
    }
    mysql_close($link);
    exit();
}

if (isset($_GET["barvy"])) {
    $idmap = $_GET["idmap"];
    $query = sprintf(SQL::$select_dyncismap_byIdCisMap, mysql_real_escape_string($idmap));
    $result = mq($query, $link);
    $activeColors;
    $count = 0;
    $lastIdVal = null;
    while ($row = mysql_fetch_array($result)) {
        if ($lastIdVal != $row["idval"]) {
            $activeColors[$count] = $row["idval"];
            $lastIdVal = $row["idval"];
            $count++;
        }
    }
    $query = sprintf(SQL::$select_ciselnik_barvy);
    $result = mq($query, $link);
    $ret = "";
    while ($row = mysql_fetch_array($result)) {
        $checked = "";
        foreach ($activeColors as $value) {
            if ($value == $row["idval"]) {
                $checked = "checked";
            }
        }
        $ret .= $row["value"] . ' <input class="colorbox" count="0" barva="' . $row["idval"] . '" type="checkbox" name="1_' . $row["idval"] . '" ' . $checked . '/><br/>';
    }
    echo $ret;
    mysql_close($link);
    exit();
}


if (isset($_GET["velikosti"])) {
    $idmap = $_GET["idmap"];
    $query = sprintf(SQL::$select_dyncismap_byIdCisMap, mysql_real_escape_string($idmap));
    $result = mq($query, $link);
    $activeSizes;
    $count = 0;
    while ($row = mysql_fetch_array($result)) {
        $activeSizes[$count] = $row["idval"];
        $count++;
    }
    $query = sprintf(SQL::$select_ciselnik_velikosti);
    $result = mq($query, $link);
    $ret = "";
    $countSizeList = 1;
    while ($row = mysql_fetch_array($result)) {
        $checked = "";
        if (isset($activeSizes)) {
            foreach ($activeSizes as $value) {
                if ($value == $row["idval"]) {
                    $checked = "checked";
                }
            }
        }
        $tmp = $row["sortNumber"] % 100;
        $tmp = ($row["sortNumber"] - $tmp)/100;
        if ($tmp != $countSizeList) {
            $ret .= '<div class="clear"><br/></div>';
            $countSizeList = $tmp;
        }
        $ret .= '<div class="floatLeft"> <input type="checkbox" class="barvaCheck" name="2_' . $row["idval"] . '" ' . $checked . '/> ' . $row["value"] . '&nbsp;&nbsp;&nbsp;&nbsp;</div>';        
    }
    echo $ret;
    mysql_close($link);
    exit();
}

if (isset($_POST["getLastImage"])) {
    echo Settings::$templateSys_path . "Image.php?size=full&id=" . getUser()->getLastImage();
    mysql_close($link);
    exit();
}

if (isset($_POST["idmap"]) && isset($_POST["idcis"])) {
    if (getUser()->getRole() == "admin") {
        $idmap = $_POST["idmap"];
        switch ($_POST["idcis"]) {
            //barva
            case 1:
                $query = sprintf(SQL::$select_dyncismap_byIdCisMap, mysql_real_escape_string($idmap));
                $result = mq($query, $link);
                $activeColors;
                $count = 0;
                $lastIdVal = null;
                while ($row = mysql_fetch_array($result)) {
                    if ($lastIdVal != $row["idval"]) {
                        $activeColors[$count] = $row["idval"];
                        $lastIdVal = $row["idval"];
                        $count++;
                    }
                }
                $query = sprintf(SQL::$select_ciselnik_barvy);
                $result = mysql_query($query, $GLOBALS["link"]);
                while ($row = mysql_fetch_array($result)) {
                    if (isset($_POST["1_" . $row["idval"]])) {
                        $count = 0;
                        $containts = 0;
                        foreach ($activeColors as $value) {
                            if ($value == $row["idval"]) {
                                $containts = 1;
                                $activeColors[$count] = null;
                            }
                            $count++;
                        }
                        if (!$containts) {
                            $newDefaultName = generateName();
                            $query = sprintf(SQL::$copyImage, mysql_real_escape_string($newDefaultName), mysql_real_escape_string(Settings::$defaultImage));
                            mysql_query($query, $GLOBALS["link"]);
                            addCiselnikMapFull($_POST["idmap"], 1, $row["idval"], $newDefaultName);
                            addCiselnikMapSklad($_POST["itemid"]);
                        }
                    }
                }
                foreach ($activeColors as $value) {
                    if ($value != null) {
                        $query = sprintf(SQL::$delete_dyncismap_byIdCisMapAndIdVal, mysql_real_escape_string($_POST["idmap"]), mysql_real_escape_string($value));
                        mq($query, $link);
                    }
                }
                break;
            //velikost
            case 2:
                $query = sprintf(SQL::$delete_dyncismap_byIdCisMap, mysql_real_escape_string($_POST["idmap"]));
                mq($query, $link);
                $query = sprintf(SQL::$select_ciselnik_velikosti);
                $result = mysql_query($query, $GLOBALS["link"]);
                while ($row = mysql_fetch_array($result)) {
                    if (isset($_POST["2_" . $row["idval"]])) {
                        addCiselnikMap($_POST["idmap"], 2, $row["idval"]);
                        addCiselnikMapSklad($_POST["itemid"]);
                    }
                }
                break;
        }
    }
    mysql_close($link);
    exit();
}

function addCiselnikMapSklad($itemid) {
    $query = sprintf(SQL::$checkSklad, mysql_real_escape_string($itemid));
    $query = mq($query, $GLOBALS["link"]);
    $velikosti;
    $vcount = 0;
    $barvy;
    $bcount = 0;
    $sklad;
    $skladIdVal;
    $scount = 0;
    while ($row = mysql_fetch_array($query)) {
        if ($row["idcis"] == 1) {
            $barvy[$bcount] = $row["idval"];
            $bcount++;
        }
        if ($row["idcis"] == 2) {
            $velikosti[$vcount] = $row["idval"];
            $vcount++;
        }
        if ($row["idcis"] == 3) {
            $sklad[$bcount] = $row["value"];
            $skladIdVal[$bcount] = $row["idval"];
            $bcount++;
        }
    }
    $query = sprintf(SQL::$select_dyncismap_byItemId, mysql_real_escape_string($itemid), "sklad");
    $query = mq($query, $GLOBALS["link"]);
    $iddyncismap = mysql_fetch_array($query);
    $iddyncismap = $iddyncismap["idcismap"];
    foreach ($barvy as $barvaCurrent) {
        $containts = 0;
        foreach ($velikosti as $velikostCurrent) {
            $containts = 0;
            foreach ($sklad as $skladCurrent) {
                if ($skladCurrent == $barvaCurrent . "_" . $velikostCurrent) {
                    $containts = 1;
                }
            }
            if ($containts == 0) {
                $query = sprintf(SQL::$select_ciselnik_byValue, mysql_real_escape_string($barvaCurrent . "_" . $velikostCurrent));
                $query = mq($query, $GLOBALS["link"]);
                $row = mysql_fetch_array($query);
                addCiselnikMapFull($iddyncismap, 3, $row["idval"], "1");
            }
        }
    }
}

mysql_close($link);
?>
